﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using SouthernLand.Common;
using SouthernLand.Common.BOL.Articles;
using SouthernLand.Common.Untilities;

namespace SouthernLand.Controls
{
    public partial class ArticlesHomePage : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<ArticleCategory> lstArticleCategories =
                ArticleCategory.GetArticleCategoryByParentID(ConvertUtility.ToInt32(EnumCategory.TinTucOnline));
            if (lstArticleCategories != null && lstArticleCategories.Count > 0)
            {
                rptCategory.DataSource = lstArticleCategories;
                rptCategory.DataBind();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            rptCategory.ItemDataBound += rptCategory_ItemDataBound;
        }

        private void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var articleCate = (ArticleCategory) e.Item.DataItem;
                if (articleCate != null)
                {
                    var hplNewsCateTitle = (HyperLink) e.Item.FindControl("hplNewsCateTitle");
                    hplNewsCateTitle.Text = articleCate.CateName;
                    hplNewsCateTitle.NavigateUrl = Globals.BuildUrlByZone(articleCate.CateID,
                                                                          Globals.ProcessString(
                                                                              Globals.UnicodeToKoDau(
                                                                                  articleCate.CateName)));
                    var rptArticles = (Repeater) e.Item.FindControl("rptArticles");
                    rptArticles.DataSource = Article.GetTop2ArticleByArticleCateID(articleCate.CateID);
                    rptArticles.ItemDataBound += rptArticles_ItemDataBound;
                    rptArticles.DataBind();
                }
            }
        }

        private void rptArticles_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var article = (Article) e.Item.DataItem;
                var hplNewsTitle = (HyperLink) e.Item.FindControl("hplNewsTitle");
                hplNewsTitle.Text = article.Title;
                hplNewsTitle.NavigateUrl = Globals.BuildUrlByNews(article.ArticleID,
                                                                  Globals.ProcessString(
                                                                      Globals.UnicodeToKoDau(article.Title)));
                var hplCateReadMore = (HyperLink) e.Item.FindControl("hplCateMore");
                if (hplCateReadMore != null)
                {
                    hplCateReadMore.NavigateUrl = Globals.BuildUrlByNews(article.ArticleID,
                                                                         Globals.ProcessString(
                                                                             Globals.UnicodeToKoDau(article.Title)));
                    hplCateReadMore.Text = ResourceManager.GetString("ReadMore");
                }
            }
        }
    }
}